<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use  think\Request;

class Myclass extends Base
{     
    
	
		
	public function Index(){
		
		 return view();
	}
	
	
	
	function Add(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["classnum"] =$request->post('classnum');
				$data["classname"] =$request->post('classname');
				$data["teacherid"] =$request->post('teacherid');
				$teachers =$request->post('teachers');
		
			
				
			if($data["id"]){
				if(db('myclass')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["mid"] = session("mid");
			
				$myclass =db('myclass')->insertGetId($data);
			    if($myclass){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('myclass')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetMyClass(){
		$where="mid=".session("mid");
		$count =db('myclass')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('myclass')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {


			$teacher = db("teacher")->where("id=".$value["teacherid"])->find();
			if($teacher){
				$value["teachername"]=$teacher["teachername"];
			}
			
			$teachers = db("teacherandclass")->where("classid=".$value["id"])->select();
			$teachersname="";
			foreach ($teachers as $key1 => &$value1) {
				$teacher = db("teacher")->where("id=".$value1["teacherid"])->find();
				if($teacher){
					$teachersname=$teachersname.$teacher["teachername"].",";
				}
			}
			$value["teachers"]=$teachersname;
			
		}
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	 public function GetMyClassList(){
		 $request = Request::instance();
		 
		 
		 $where="mid=".session("mid");
		$list = db('myclass')->where($where)->select();
	
		return json($list);
		
	}
	function Del(){
		$request = Request::instance();
		$id =$request->post('id');
		
			$where = 'id='.$id;
		   $list=db('teacherandclass')->where("classid=".$id)->delete();
		   $list=db('myclass')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	function classjieye(){
		$request = Request::instance();
		$id =$request->post('id');
	

		$updateresult = Db::execute("delete from studentandparent where studentid in (select id from student where classid=? and state=0)",[$id]);
		$updateresult = Db::execute('update student set state=-1,jieyedate=? where classid=?',[date("Y-m-d h:i:sa"),$id]);
		if($updateresult!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }

	}
	function zhuanban(){
		$request = Request::instance();

		if($request->isPost()){
			$students =$request->post("students");
			$chooseclassid = $request->post("chooseclassid");
			
			$updateresult = Db::execute('update student set  classid='.$chooseclassid.' where id in('.$students.')');
			if($updateresult!==false) {
				return ['status'=>200,'message'=>'操作成功'];
			}else{
				return ['status'=>500,'message'=>'操作失败'];
			}

		}else{

			$classid =$request->get('id');
			$this->assign('classid',$classid);
			return view();
		}
	
	}
}
